#region Imported Namespaces

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Text;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using PHSRAG.Insight21.SearchContext;

using Utility = PHSRAG.Utility;
using UserContext = PHSRAG.Insight21.Context.UserContext;
using Policy = PHSRAG.Insight21.Policy;
using Annunciator = PHSRAG.WebControls.Annunciator;
using Pager = PHSRAG.WebControls.Pager;
using DataAccess = PHSRAG.Utility.DataAccess;
using Header = PHSRAG.Insight21.InsightWebControls.Header;
using PurchasingSubNavigation = PHSRAG.Insight21.InsightWebControls.PurchasingSubNavigation;
using InsightDownloadBar = PHSRAG.Insight21.InsightWebControls.InsightDownloadBar;
using RefreshDates = PHSRAG.Insight21.InsightWebControls.RefreshDates;
using ExpandingPanel = PHSRAG.WebControls.ExpandingPanel;
using CommonSearch = PHSRAG.Insight21.InsightWebControls.CommonSearch;
using PurchasingSearch = PHSRAG.Insight21.InsightWebControls.PurchasingSearch;
using ExceptionReport = PHSRAG.Utility.ExceptionReport;
using InstanceCreator = PHSRAG.Utility.InstanceCreator;
using POAPSummaries = PHSRAG.Insight21.Purchasing.POAPSummaries;
using JournalSummaries = PHSRAG.Insight21.Purchasing.JournalSummaries;
using Transactions = PHSRAG.Insight21.Purchasing.Transactions;
using PurchasingSearchKey = PHSRAG.Insight21.InsightUtilities.Constants.PurchasingSearchKey;
using SummaryScreenElements = PHSRAG.Insight21.Purchasing.Enumerations.SummaryScreenElements;

#endregion
namespace PHSRAG.Insight21.InsightWebControls
{
    /// <summary>
    ///	DisplayTransaction is the base class for transaction controls used to display
    ///	people soft transactions,
    ///	Class implements the default behavour, and derived class will if required override them
    ///	methods and properties.
    /// </summary>
    public partial class DisplayTransaction : UserControl
    {
        #region Constants
        protected const string FailedActionCommand = "FailedAction";
        #endregion

        #region Keys
        // Local Keys assocated with this control, all keys will have the same guid, 
        // to logically group them together..
        private const string CurrentPageIndexKey = "CurrentPageIndex_12B90EAE-E704-444a-8733-800F3F39866C";
        private const string AssemblyNameKey = "AssemblyNameKey_12B90EAE-E704-444a-8733-800F3F39866C";
        private const string ClassNameKey = "ClassNameKey_12B90EAE-E704-444a-8733-800F3F39866C";
        private const string SearchCriteriaVersionKey = "SearchCriteriaVersion_12B90EAE-E704-444a-8733-800F3F39866C";
        private const string SearchCriteriaLevelKey = "SearchCriteriaLevel_12B90EAE-E704-444a-8733-800F3F39866C";

        #endregion

        #region Private Instance variables
        private string incomingFundNumber;
        /// <summary>
        /// transactionKey is used to store objects/variable to be used across page loads.
        /// </summary>
        private string transactionKey;

        private bool resetSortingAndPaging;

        /// <summary>
        /// Used to track which module is currently active.
        /// </summary>
        private string moduleName = string.Empty;

        #endregion

        #region Protected Instance variables
        /// <summary>
        /// Cache Policy used to store the state of middlware objects
        /// </summary>
        protected Policy.ICachePolicy cachePolicy;
        /// <summary>
        /// Cache Policy used to store the state of Page level objects
        /// </summary>
        protected Policy.ICachePolicy pageCachePolicy;
        /// <summary>
        /// variable used to check the if resultset is obtained for search
        /// </summary>
        protected bool searchInEffect;

        #endregion

        #region Events
        /// <summary>Get/Set handler for the event raised when the requested action fails</summary>
        public event CommandEventHandler FailedAction
        {
            add { Events.AddHandler(FailedActionCommand, value); }
            remove { Events.RemoveHandler(FailedActionCommand, value); }
        }

        #endregion

        #region Constructors
        public DisplayTransaction() { }

        /// <summary>
        /// Intilize the transaction Key with the concrete class keys
        /// </summary>
        /// <param name="key">Derived controls Key</param>
        public DisplayTransaction(string key)
        {
            transactionKey = key;
        }

        #endregion

        #region Protected Properties

        #region Current Page Index
        /// <summary>
        /// Get/Set the current page index in pagecache policy, 
        /// Derive class datagrid use this to set the Current page index for display. 
        /// This information persists across page loads.
        /// </summary>
        protected int CurrentPageIndex
        {
            get
            {
                Hashtable h = pageCachePolicy.GetCachedObject(transactionKey) as Hashtable;
                return (h == null || h[CurrentPageIndexKey] == null) ? 0 : (int)h[CurrentPageIndexKey];
            }
            set
            {
                Hashtable h = pageCachePolicy.GetCachedObject(transactionKey) as Hashtable;
                if (h == null)
                    h = new Hashtable();
                h[CurrentPageIndexKey] = value;

                pageCachePolicy.Cache(transactionKey, h);
            }
        }

        #endregion

        #region SearchCriteriaLevel
        /// <summary>
        /// Get/Set the Search Criteria level for page. 
        /// </summary>
        public SearchCriteria.Level SearchCriteriaLevel
        {
            get
            {
                object o = pageCachePolicy.GetCachedObject(String.Format("{0}_{1}", ModuleName, SearchCriteriaLevelKey));
                return (o == null) ? SearchCriteria.Level.Summary : (SearchCriteria.Level)Enum.Parse(typeof(SearchCriteria.Level), o.ToString());
            }
            set
            {
                pageCachePolicy.Cache(String.Format("{0}_{1}", ModuleName, SearchCriteriaLevelKey), value);
            }
        }
        #endregion

        #region  Search Criteria Version and its individual criterias

        #region  Search Criteria Version

        /// <summary>
        /// Get/Set the Search Criteria Version for page. 
        /// if the search criteria version hasn't changed from last time, 
        /// control gets the business object/Information from cache.
        /// </summary>
        public int SearchCriteriaVersion
        {
            get
            {
                Hashtable h = pageCachePolicy.GetCachedObject(String.Format("{0}_{1}", ModuleName, SearchCriteriaVersionKey)) as Hashtable;
                return (h == null || h[SearchCriteriaVersionKey] == null) ? Purchasing.Constants.SearchVersionWhenCriteriaNotSet : (int)h[SearchCriteriaVersionKey];
            }
            set
            {
                Hashtable h = pageCachePolicy.GetCachedObject(String.Format("{0}_{1}", ModuleName, SearchCriteriaVersionKey)) as Hashtable;
                if (h == null)
                    h = new Hashtable();
                h[SearchCriteriaVersionKey] = value;
                pageCachePolicy.Cache(String.Format("{0}_{1}", ModuleName, SearchCriteriaVersionKey), h);
            }
        }

        # endregion

        # endregion

        #endregion

        #region Public Properties

        /// <summary>
        /// Used to track which module is currently active.
        /// </summary>
        public string ModuleName
        {
            get
            {
                if (string.IsNullOrEmpty(moduleName))
                {
                    throw new Exception("ModuleName not set in DisplayTransaction control.");
                }

                return moduleName;
            }
            set
            {
                moduleName = value;
            }
        }

        #region Incomming Fund Number
        /// <summary>
        /// Gets/Sets incoming fund number when navigationg from others pages to this page
        /// </summary>
        public string IncomingFundNumber
        {
            get
            {
                return incomingFundNumber;
            }
            set
            {
                incomingFundNumber = value;
            }
        }
        #endregion

        #region Assembly Name and Class name
        /// <summary>
        /// Get/Set property for Assembly Name, POAPSummary or Transaction page sets the value for this property
        /// to the assembly name for POAPSummaries or JournalSummaries classes,
        /// this property is stored in pageCache to be utilized across page loads.
        /// </summary>
        public string AssemblyName
        {
            get
            {
                Hashtable h = pageCachePolicy.GetCachedObject(transactionKey) as Hashtable;
                return (h == null || h[AssemblyNameKey] == null) ? string.Empty : h[AssemblyNameKey].ToString();
            }
            set
            {
                Hashtable h = pageCachePolicy.GetCachedObject(transactionKey) as Hashtable;
                if (h == null)
                    h = new Hashtable();
                h[AssemblyNameKey] = value;
                pageCachePolicy.Cache(transactionKey, h);
            }
        }

        /// <summary>
        /// Get/Set property for Class Name, POAPSummary or Transaction page sets the value for this property
        /// to the either POAPSummaries or JournalSummaries classes based on what logged users has set 
        /// the Order Select View radio buttions to. 
        /// this property is stored in pageCache to be utilized across page loads.
        /// </summary>
        public string ClassName
        {
            get
            {
                Hashtable h = pageCachePolicy.GetCachedObject(transactionKey) as Hashtable;
                return (h == null || h[ClassNameKey] == null) ? string.Empty : h[ClassNameKey].ToString();
            }
            set
            {
                Hashtable h = pageCachePolicy.GetCachedObject(transactionKey) as Hashtable;
                if (h == null)
                    h = new Hashtable();
                h[ClassNameKey] = value;
                pageCachePolicy.Cache(transactionKey, h);
            }
        }
        #endregion

        #region PeopleSoftTransaction
        /// <summary>
        ///	Retreive tbe business Object of transaction type, 
        ///	derived class needs to implemenet this property, otherwise null is returned.
        /// </summary>
        public virtual Transactions PeopleSoftTransaction
        {
            get { return null; }
        }
        #endregion

        public bool ResetSortingAndPaging
        {
            get
            {
                return resetSortingAndPaging;
            }
            set
            {
                resetSortingAndPaging = value;
            }
        }

        #endregion

        #region Protected methods

        /// <summary>
        /// Virtual method to Load the datagrid with data for display within one page of the data grid.
        /// Throw exception if derive class doesn't override this method.
        /// </summary>
        protected virtual void LoadDataGrid()
        {
            throw new Exception("Derive classes need to implement this method");
        }
        /// <summary>
        /// Virtual method to Manages the Page Control links to support pagination.
        /// Throw exception if derive class doesn't override this method.
        /// </summary>
        protected virtual void ManagePagination()
        {
            throw new Exception("Derive classes need to implement this method");
        }
        /// <summary>
        /// The page calls this method to load controls, intilize the CurrentPageIndex every time 
        /// search buttion is clicked, 
        /// Throw exception if derive class doesn't override this method.
        /// </summary>
        public virtual void LoadControl()
        {
            throw new Exception("Derive classes need to implement this method");
        }
        #endregion

        #region EventHandlers

        #region Pager Above and Below Datagrid events

        /// <summary>
        /// Event Handler for the FirstPage link on the Pager control.
        /// Derive class needs to implement this event handlers
        /// Throw exception if derive class doesn't override this event
        /// </summary>
        /// <param name="sender">Control which rasied the event</param>
        /// <param name="e">Arguments associated with the event</param>
        protected virtual void OnFirstPage(object sender, CommandEventArgs e)
        {
            throw new Exception("Derive classes need to implement this event handler");
        }

        /// <summary>
        /// Event Handler for the LastPage link on the Pager control.
        /// Throw exception if derive class doesn't override this event
        /// </summary>
        /// <param name="sender">Control which rasied the event</param>
        /// <param name="e">Arguments associated with the event</param>
        protected virtual void OnLastPage(object sender, CommandEventArgs e)
        {
            throw new Exception("Derive classes need to implement this event handler");
        }

        /// <summary>
        /// Event Handler for the NextPage link on the Pager control.
        /// Throw exception if derive class doesn't override this event
        /// </summary>
        /// <param name="sender">Control which rasied the event</param>
        /// <param name="e">Arguments associated with the event</param>
        protected virtual void OnNextPage(object sender, CommandEventArgs e)
        {
            throw new Exception("Derive classes need to implement this event handler");
        }

        /// <summary>
        /// Event Handler for the PreviousPage link on the Pager control..
        /// Throw exception if derive class doesn't override this event
        /// </summary>
        /// <param name="sender">Control which rasied the event</param>
        /// <param name="e">Arguments associated with the event</param>
        protected virtual void OnPreviousPage(object sender, CommandEventArgs e)
        {
            throw new Exception("Derive classes need to implement this event handler");
        }

        #endregion

        #region Sort
        /// <summary>
        /// Handler for the event raised when a column header within the data grid is clicked.
        /// Throw exception if derive class doesn't override this event
        /// </summary>
        /// <param name="source">Control which raised the event</param>
        /// <param name="e">Argument associated with the event</param>
        protected virtual void OnSort(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
        {
            throw new Exception("Derive classes need to implement this event handler");
        }
        #endregion

        #region Load

        #endregion
        #endregion

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            //InitializeComponent();
            base.OnInit(e);

            cachePolicy = (Policy.ICachePolicy)Session[Policy.Names.CachePolicyKey];
            pageCachePolicy = (Policy.ICachePolicy)Session[Policy.Names.PageCachePolicyKey];
        }

        #endregion
    }
}
// --- EOF ---